package com.daocheng.weblog.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.daocheng.weblog.admin.model.vo.dashboard.FindDashboardPVStatisticsInfoRspVO;
import com.daocheng.weblog.admin.model.vo.dashboard.FindDashboardStatisticsInfoRspVO;
import com.daocheng.weblog.admin.service.AdminDashboardService;
import com.daocheng.weblog.common.domain.dos.ArticleDO;
import com.daocheng.weblog.common.domain.dos.ArticlePublishCountDO;
import com.daocheng.weblog.common.domain.dos.StatisticsArticlePVDO;
import com.daocheng.weblog.common.domain.mapper.ArticleMapper;
import com.daocheng.weblog.common.domain.mapper.CategoryMapper;
import com.daocheng.weblog.common.domain.mapper.StatisticsArticlePVMapper;
import com.daocheng.weblog.common.domain.mapper.TagMapper;
import com.daocheng.weblog.common.utils.Response;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName AdminDashboardServiceImpl
 * @author: 稻城
 * @date: 2025/3/15
 * @Version 1.0
 * @description: TODO
 */
@Service
@Slf4j
public class AdminDashboardServiceImpl implements AdminDashboardService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private StatisticsArticlePVMapper statisticsArticlePVMapper;

    @Override
    public Response findDashboardStatistics() {
        //查询文章总数
        Long articleTotalCount = articleMapper.selectCount(Wrappers.emptyWrapper());
        //获取分类总数
        Long categoryTotalCount = categoryMapper.selectCount(Wrappers.emptyWrapper());
        //获取标签总数
        Long tagTotalCount = tagMapper.selectCount(Wrappers.emptyWrapper());
        //总浏览量
        List<ArticleDO> articleDOS = articleMapper.selectAllReadNum();
        //获取访问总数
        Long pvTotalCount = 0L;
        if (!CollectionUtils.isEmpty(articleDOS)) {
            pvTotalCount = articleDOS.stream().mapToLong(ArticleDO::getReadNum).sum();
        }
        //组装vo数据
        FindDashboardStatisticsInfoRspVO vo = FindDashboardStatisticsInfoRspVO.builder()
                .articleTotalCount(articleTotalCount)
                .categoryTotalCount(categoryTotalCount)
                .tagTotalCount(tagTotalCount)
                .pvTotalCount(pvTotalCount)
                .build();
        return Response.success(vo);
    }

    @Override
    public Response findDashboardPublishArticleStatistics() {
        //获取当前日期
        LocalDate currDate = LocalDate.now();
        //当前日期倒退一年
        LocalDate startDate = currDate.minusYears(1);
        //获取这一年内每日发布文章的数量
        List<ArticlePublishCountDO> articlePublishCountDOS = articleMapper.selectPublishCountGroupByDate(startDate, currDate);
        Map<LocalDate, Long> map = null;
        if (!CollectionUtils.isEmpty(articlePublishCountDOS)) {
            //DO 转 MAP
            Map<LocalDate, Long> dateArticleCount = articlePublishCountDOS.stream()
                    .collect(Collectors.toMap(ArticlePublishCountDO::getDate, ArticlePublishCountDO::getCount));
            //有序map,返回的日期文章数升序排列
            map = Maps.newLinkedHashMap();
            //从上一年的今天循环到今天
            for (; startDate.isBefore(currDate) || startDate.isEqual(currDate); startDate = startDate.plusDays(1)) {
                Long count = dateArticleCount.get(startDate);
//                map.put(startDate, map.getOrDefault(startDate, 0L));
                map.put(startDate, Objects.isNull(count) ? 0L : count);
            }
        }
        return Response.success(map);
    }

    @Override
    public Response findDashboardPVStatistics() {
        List<StatisticsArticlePVDO> list = statisticsArticlePVMapper.selectLatestWeekRecords();
        Map<LocalDate, Long> pvDateCountMap = Maps.newHashMap();
        if (!CollectionUtils.isEmpty(list)) {
            //转Map
            pvDateCountMap = list.stream()
                    .collect(Collectors.toMap(StatisticsArticlePVDO::getPvDate, StatisticsArticlePVDO::getPvCount));
        }
        FindDashboardPVStatisticsInfoRspVO vo = null;
        //日期集合
        List<String> pvDates = Lists.newArrayList();
        //PV集合
        List<Long> pvCounts = Lists.newArrayList();;
        //当前日期
        LocalDate currDate = LocalDate.now();
        //一周前
        LocalDate startDate = currDate.minusDays(7);
        for (; startDate.isBefore(currDate) || startDate.isEqual(currDate); startDate = startDate.plusDays(1)) {
            Long count = pvDateCountMap.get(startDate);
            pvDates.add(startDate.toString());
            pvCounts.add(Objects.isNull(count) ? 0L : count);
        }
       vo = FindDashboardPVStatisticsInfoRspVO.builder()
                .pvDates(pvDates)
                .pvCounts(pvCounts)
                .build();
        return Response.success(vo);
    }
}
